import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { BaseCard, CardType } from './BaseCard';
import { ResMgr } from '../Mgr/ResMgr';
import { GAMEDATA } from './GAMEDATA';
import { GameScene } from './GameScene';
import { CatCardLoad } from './CatCardLoad';
import { Tools } from '../Tools';
import { Global } from '../Global';
const { ccclass, property } = _decorator;

@ccclass('CatCard')
export class CatCard extends BaseCard {

    //猫咪
    @property(Sprite)
    private Cat: Sprite = null;

    //红心
    @property(Node)
    private RedHeart: Node = null;

    //血量
    @property(Label)
    private HeartText: Label = null;
    public HeartNum: number = 0;

    //背景
    @property(Node)
    private Back: Node = null;

    /**
     * 更新卡牌
     */
    public UpdateCard(_cardtype: CardType): void {
        super.UpdateCard(_cardtype);

        this.Back.active = false;
        this.BigNum.node.active = false;

        this.RedHeart.parent.active = true;
        this.RedHeart.active = false;

        this.Cat.spriteFrame = ResMgr.Instance.SpriteFrames["game_cat" + this.CardInfo.color.toString()];

        this.HeartNum = GAMEDATA.GAME_CONIFG[GAMEDATA.GAME_DIFFICULTY][this.CardInfo.num - 10];
        this.HeartText.string = this.HeartNum.toString();

    }

    /**
     * 更新血量
     */
    public UpdateHeartNum(_cardtype: CardType): void {

        if (_cardtype.num == 0) {
            if (this.HeartNum == 1) {
                this.HeartNum -= _cardtype.num + 1;
                Global.HurtScore += _cardtype.num + 1;
            } else {
                Global.HurtScore += this.HeartNum - 1;
                this.HeartNum = 1;
            }
        } else {
            this.HeartNum -= _cardtype.num + 1;
            Global.HurtScore += _cardtype.num + 1;

            if (this.HeartNum < 0) {
                Global.HurtOver += this.HeartNum;
            }

        }

        let ani_name: string = "JokerEffect";

        if (this.HeartNum <= 0 && _cardtype.color != this.CardInfo.color) ani_name = "JokerEffect1"

        Tools[ani_name](this.node, 0.1, () => {
            if (this.HeartNum <= 3) this.RedHeart.active = true;

            if (this.HeartNum <= 0 && _cardtype.color == this.CardInfo.color) {
                let show_cat: Node[] = [];

                for (let cat of this.node.parent.children) {
                    if (cat.active) show_cat.push(cat)
                };

                if (show_cat.length > 1) {
                    GameScene.Instance.CatCardLoad.getComponent(CatCardLoad).UpdateCat(this.node);
                    this.node.parent.children[show_cat.length - 2].active = false;
                } else {
                    this.node.active = false;
                }

                //猫咪加分
                Global.CatGiveScore += GAMEDATA.GAME_CONIFG[GAMEDATA.GAME_DIFFICULTY][this.CardInfo.num - 10];

                //判断游戏是否结束
                this.node.parent.parent.getComponent(CatCardLoad).CheckGameOver();

            }

            if (this.HeartNum < 1) {
                this.HeartNum = 1;
            }
            this.HeartText.string = this.HeartNum.toString();

        });



    }

    /**
     * 我是背景
     */
    public IamBack() {
        this.Back.active = true;
        this.RedHeart.parent.active = false;
    }

}


